<?php

class CopyController extends Controller
{
	private $_adModel;

	public function init(){
		parent::init();
		$this->defaultAction = 'cover';
	}
	
	public function actionCover(){
		$criteria=new CDbCriteria(array(
			'condition'=>'status in ('.Ad::STATUS_ACTIVE.','.Ad::STATUS_PAUSED.')',
			'order'=>'status ASC,budget,date DESC',
		));
		$ads = Ad::model()->with('uvCount','spentCount')->findAll($criteria);
		$this->render('cover',array('ads'=>$ads));
	}
	
	public function actionAd(){
		$adModel = $this->loadAdModel();
		$this->render('ad',array('ad'=>$adModel));
	}
	
	public function actionHowto(){
		$this->render('howto');
	}
	
	public function actionSlogans(){
		$this->render('slogans');
	}
	
	public function actionEarnings(){
		$this->render('earnings');
	}
	
	public function actionProbono(){
		$this->render('probono');
	}
	
	public function loadAdModel(){
		if($this->_adModel===null)
		{
			if(isset($_GET['ad_id']))
			{
				$this->_adModel=Ad::model()->with('uvCount','spentCount')->findByPk($_GET['ad_id']);
			}
			if($this->_adModel===null || $this->_adModel->status == Ad::STATUS_REMOVED)
				throw new CHttpException(404,'The requested page does not exist.');
		}
		return $this->_adModel;
	}
}